package com.gutsyzhan.activitifly;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
public class Part4_Task_Claim {
    @Resource
    private TaskService taskService;
    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;

    /**
     * 通过BPMN部署流程
     */
    @Test
    public void initDeployment(){
        String fileName = "BPMN/Part4_Task_Claim.bpmn20.xml";
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource(fileName)
                .name("流程部署候选人流程")
                .deploy();
        System.out.println(deployment.getName());
    }

    /**
     * 查询流程定义
     */
    @Test
    public void getDefinitions() {
        List<ProcessDefinition> definitions = repositoryService.createProcessDefinitionQuery().list();
        for (ProcessDefinition definition : definitions) {
            System.out.println("Key:" + definition.getKey());
        }
    }

    /**
     * 初始化流程实例
     */
    @Test
    public void initProcessInstance() {
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_Claim", "bKey003");
        System.out.println("流程实例id："+ processInstance.getProcessInstanceId());
    }

    /**
     * 查询所有的任务
     * (所有的查询都是createXXXQuery)
     */
    @Test
    public void getTasks(){
        List<Task> tasks = taskService.createTaskQuery().list();
        for (Task task : tasks) {
            System.out.println("id:" + task.getId());
            System.out.println("name:" + task.getName());
            System.out.println("assignee:" + task.getAssignee());
        }
    }

    /**
     * 查询我的代办任务
     */
    @Test
    public void getTasksByAssignee(){
        List<Task> tasks = taskService.createTaskQuery()
                .taskAssignee("wukong")
                .list();
        for (Task task : tasks) {
            System.out.println("id:" + task.getId());
            System.out.println("name:" + task.getName());
            System.out.println("assignee:" + task.getAssignee());
        }
    }

    /**
     * 执行任务
     */
    @Test
    public void completeTask(){
        taskService.complete("ccf0a388-9114-11ee-8cae-f88c2116e1b9");
        System.out.println("执行任务");
    }

    /**
     * 拾取任务
     */
    @Test
    public void claimTask(){
        TaskQuery taskQuery = taskService.createTaskQuery().taskId("b85bcb4d-9118-11ee-bf54-f88c2116e1b9");
        taskService.claim("b85bcb4d-9118-11ee-bf54-f88c2116e1b9", "bajie");
    }

    /**
     * 归还与交办任务
     */
    @Test
    public void setTaskAssignee(){
        TaskQuery taskQuery = taskService.createTaskQuery().taskId("b85bcb4d-9118-11ee-bf54-f88c2116e1b9");
        taskService.setAssignee("b85bcb4d-9118-11ee-bf54-f88c2116e1b9", "null");
    }
}
